package com.qf.agro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.agro.common.ResultInfo;
import com.qf.agro.mapper.CompanyMapper;
import com.qf.agro.pojo.Company;
import com.qf.agro.service.CompanyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author 10202
 * @version V1.0
 * @Project smart-agriculture-parent
 * @Package com.qf.agro.service.impl
 * @Date 2023/5/22 20:30
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper,Company> implements CompanyService, IService<Company> {

    private final CompanyMapper companyMapper;

    public CompanyServiceImpl(CompanyMapper companyMapper) {
        this.companyMapper = companyMapper;
    }


    @Override
    public ResultInfo getCompanyList(Long pageNum, Long pageSize, String name) {

        LambdaQueryWrapper<Company> lqw=new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name),Company::getName,name);
        //状态
        lqw.eq(Company::getState,1);
        IPage<Company> iPage=new Page<>(pageNum,pageSize);
         iPage= companyMapper.selectPage(iPage, lqw);
        return ResultInfo.success(iPage);
    }

    @Override
    public ResultInfo updateCompany(Company company) {

        return this.updateById(company)?ResultInfo.success("更新成功"):ResultInfo.error("更新失败");
    }

    @Override
    public ResultInfo addCompany(Company company) {


        return this.save(company)?ResultInfo.success("添加成功"):ResultInfo.error("添加失败");
    }

    @Override
    public ResultInfo deleteCompany(String id) {
        LambdaQueryWrapper<Company> lqw=new LambdaQueryWrapper();
        lqw.eq(Company::getId,id);
        Company company = this.getOne(lqw);
        company.setState(0);

        return this.update(company,lqw)?ResultInfo.success("删除成功"):ResultInfo.error("删除失败");
    }

}
